﻿using System;

namespace CommonFramework.Time
{
    public static class TimeUtil
    {
        /// <summary>
        /// 当前的unix时间搓,单位秒
        /// </summary>
        public static long NowTimestampSeconds()
        {
            return  ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds();
        }

        /// <summary>
        /// 当前的unix时间搓,单位秒
        /// </summary>
        public static double NowTimestampSecondsDouble()
        {
            return NowTimestampMilliseconds() * 0.001;
        }

        /// <summary>
        /// 当前的unix时间搓,单位毫秒
        /// </summary>
        public static long NowTimestampMilliseconds()
        {
            return ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds();
        }

        public static double GameStartTimepoint { get; private set; }
        public static double Now { get; private set; }
        public static double TimeSinceGameStart => Now - GameStartTimepoint;
        public static double LastUpdateTimepoint { get; private set; }
        public static double DeltaTime { get; private set; }

        public static void OnStart()
        {
            Now= NowTimestampSecondsDouble();
            GameStartTimepoint = Now;
            LastUpdateTimepoint = Now;
        }
        public static void OnUpdate()
        {
            Now = NowTimestampSecondsDouble();
            DeltaTime = Now - LastUpdateTimepoint;
            LastUpdateTimepoint = Now;
        }
    }
}